﻿using Foxit.PDF;
using Foxit.PDF.PageElements;
using Sustainalytics.Reporting.Framework;
using System;

namespace Sustainalytics.ReportGenerator.BL
{
    public class TearSheetDisclaimersSection : PdfReportSectionGenerator
    {
        private const float _sectionNameFontSize = 12f;

        public override void ComposeSectionContent()
        {
            const float startPosition = 130f;

            var page = _report.NewPage();
            _report.Pages.Add(page);

            AddHeaderToPage(page);

            AddDisclaimers(page, startPosition);

            _outlinePositions.Add(new Reporting.Framework.OutlinePosition { Name = "Disclaimers", PageNumber = 1 });
        }

        private void AddDisclaimers(Page page, float top)
        {
            const float aboutTextWidth = 200f;
            const float disclaimerTextWidth = 325f;

            const string aboutTitle = "<p><b>ABOUT SUSTAINALYTICS</b></p>";
            const string disclaimerTitle = "<p><b>DISCLAIMER</b></p>";
            const string aboutText = "<p>Sustainalytics is an award-winning global responsible investment research firm specialized in environmental, social and governance(ESG) research and analysis. The firm offers global perspectives and solutions that are underpinned by local expertise, serving both values - based and mainstream investors that integrate ESG information and assessments into their investment decisions.</p>";
            string disclaimerText = $"<p><b>Copyright © {DateTime.UtcNow.Year} Sustainalytics. All rights reserved.</b></p><p>Sustainalytics is solely the ESG provider and it does not represent or warrant the appropriateness of the use of any such information for any investment decision and any investment decisions are the sole responsibility of the users of the Scotia iTRADE platform. The copying, republication or redistribution of the information contained herein, including by framing, scanning, copying or other similar methods is expressly prohibited without the prior written consent of Sustainalytics. Sustainalytics accepts no liability for the accuracy or completeness of the information or software furnished through the platform, or for delays, interruptions or omissions therein nor for any lost profits, indirect, special or consequential damages or any other damages.</p>";


            var style = new FormattedTextAreaStyle(Fonts.CalibriFontFamily, 8f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 5, AllowOrphanLines = false }
            };


            var aboutTextArea = new FormattedTextArea(aboutText, 0.0f, 0.0f, aboutTextWidth, 200f, style);
            var aboutTextHeight = aboutTextArea.GetRequiredHeight() + 5f;

            var disclaimerTextArea = new FormattedTextArea(disclaimerText, 0.0f, 0.0f, disclaimerTextWidth, 200f, style);
            var disclaimerTextHeight = disclaimerTextArea.GetRequiredHeight() + 5f;



            page.Elements.Add(new FormattedTextArea(aboutTitle, _report.LeftMargin, top, aboutTextWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));
            page.Elements.Add(new FormattedTextArea(disclaimerTitle, _report.LeftMargin + 220f, top, aboutTextWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));

            top += 25f;

            page.Elements.Add(new FormattedTextArea(aboutText, _report.LeftMargin, top, aboutTextWidth, aboutTextHeight, style));
            page.Elements.Add(new FormattedTextArea(disclaimerText, _report.LeftMargin + 220f, top, disclaimerTextWidth, disclaimerTextHeight, style));

            top += aboutTextHeight;

            const string text = "www.sustainalytics.com";
            var font = Fonts.CalibriBold;

            var label = new Label(text, _report.LeftMargin, top, 100f, 10f, font, 8f, RgbColor.Black);

            var action = new UrlAction("www.sustainalytics.com");
            var link = new Link(_report.LeftMargin, top, font.GetTextWidth(text, 8f), 10f, action);

            var elements = new Group {label, link};

            page.Elements.Add(elements);

        }
    }
}
